__type__ = "DB"
__instance__ = "PyDB"

import urllib
import urllib2
import urlparse
import logging
import re

TORRENT_URL, FILENAME, TITLE = range(3)

class PyDB:

    """
        Adds support for db fansubs 'unique' feed
    """

    def run(self, source, patterns, config, session):
        page = urllib2.urlopen("http://www.dattebayo.com/t/dump")
        matches = []
        for line in page.readlines():
            data = line.split(",")
            for pattern in patterns:
                regexp = str(pattern['pattern'])
                # check if pattern matches
                if re.search(regexp, data[TITLE], re.IGNORECASE|re.UNICODE) or re.search(regexp, data[FILENAME], re.IGNORECASE|re.UNICODE):
                    match = pattern.copy()
                    match['torrent'] = data[TORRENT_URL]
                    match['title'] = data[TITLE]
                    matches.append(match)
        return matches
